package com.huan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huan.VO.*;
import com.huan.constant.SystemConstants;
import com.huan.domain.ResponseResult;
import com.huan.domain.entity.Article;
import com.huan.domain.entity.ArticleTag;
import com.huan.domain.entity.Category;
import com.huan.mapper.ArticleMapper;
import com.huan.service.ArticleService;
import com.huan.service.ArticleTagService;
import com.huan.service.CategoryService;
import com.huan.utils.BeanCopyUtil;
import com.huan.utils.RedisCache;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {
    @Autowired
    CategoryService categoryService;
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private ArticleTagService articleTagService;
    @Autowired
    private  ArticleMapper articleMapper;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public ResponseResult hotArticleList() {
//        1.实现降序查询
//        2.草稿不能展示
//        3.删除了的不能展示、
//        4.最多只有十个数据
        LambdaQueryWrapper<Article> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Article::getViewCount);
        wrapper.eq(Article::getDelFlag, SystemConstants.ARTICLE_STATUS_NORMAL);
        wrapper.eq(Article::getStatus,SystemConstants.ARTICLE_STATUS_NORMAL);
        Page<Article> articlePage = new Page<>(1,10);
        List<Article> list = page(articlePage, wrapper).getRecords();
        List<HotArticleVo> articleVos = BeanCopyUtil.resultAllVo(list, HotArticleVo.class);
        ResponseResult result = ResponseResult.okResult(articleVos);














        return result;
    }

    @Override
    public ResponseResult articleList(Integer pageNum, Integer pageSize, Long categoryId) {
//        1.查询状态为0的文章
//        2.如果前端传进来categoryId，就查询出对应Id的文章数据
//        3.根据前端传过来的数据进行分页查询
//        4.置顶的文章放在前面
        LambdaQueryWrapper<Article> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Objects.nonNull(categoryId)&&categoryId>0,Article::getCategoryId,categoryId);
        wrapper.eq(Article::getStatus,SystemConstants.ARTICLE_STATUS_NORMAL);
        wrapper.orderByDesc(Article::getIsTop);
        Page<Article> page = new Page<>(pageNum,pageSize);
        Page<Article> page1 = page(page, wrapper);
        List<Article> list = page1.getRecords();
//        for (Article article : list) {
//            Category category = categoryService.getById(article.getCategoryId());
//            article.setCategoryName(category.getName());
//        }
        list.stream()
                .map(article -> article.setCategoryName(categoryService.getById(article.getCategoryId()).getName()))
                .collect(Collectors.toList());
        List<ArticleListVo> articleListVos = BeanCopyUtil.resultAllVo(list, ArticleListVo.class);
        PageVo pageVo = new PageVo(articleListVos,page.getTotal());
        ResponseResult result = ResponseResult.okResult(pageVo);


        return result;
    }

    @Override
    public ResponseResult getArticleDetail(Long id) {
//        根据id查到文章详情content
//        在返回数据中需要有CategoryName
        Article article = getById(id);
        ArticleDetailVo articleDetailVo = BeanCopyUtil.resultVO(article, ArticleDetailVo.class);
        Category category = categoryService.getById(articleDetailVo.getCategoryId());
        articleDetailVo.setCategoryName(category.getName());
        ResponseResult result = ResponseResult.okResult(articleDetailVo);


        return result;
    }

    @Override
    public ResponseResult updateViewCount(Long id) {
        redisCache.incrementCacheMapValue("ArticleViewCount",id.toString(),1L);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult add(AddArticleDto article) {
        rabbitTemplate.convertAndSend("my.exchange", "#", article);
        return receive(article);
//        save(BeanCopyUtil.resultVO(article,Article.class));
//        List<ArticleTag> tagList = article.getTags().stream()
//                .map(aLong -> new ArticleTag(article.getId(), aLong))
//                .collect(Collectors.toList());
//         articleTagService.saveBatch(tagList);
//        return ResponseResult.okResult();
    }
    @RabbitListener(queues = "my.queue")
    public ResponseResult receive(AddArticleDto article) {
        save(BeanCopyUtil.resultVO(article,Article.class));
        List<ArticleTag> tagList = article.getTags().stream()
                .map(aLong -> new ArticleTag(article.getId(), aLong))
                .collect(Collectors.toList());
        articleTagService.saveBatch(tagList);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getAllList(Integer pageNum, Integer pageSize, String title, String summary) {
        LambdaQueryWrapper<Article> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Article::getTitle,title);
        wrapper.in(Article::getSummary,summary);
        Page<Article> page = new Page<>();
        page.setPages(pageNum);
        page.setSize(pageSize);
        List<Article> list = page(page, wrapper).getRecords();
        List<ArticleDetailVo> detailVos = BeanCopyUtil.resultAllVo(list, ArticleDetailVo.class);
        PageVo pageVo = new PageVo(detailVos, page.getTotal());
        return ResponseResult.okResult(pageVo);



    }

    @Override
    public ResponseResult getList(Long id) {
//        LambdaQueryWrapper<Article> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(Article::getId,id);
//        List<Article> list = list(wrapper);
        Article article = getById(id);

        AddArticleDto addArticleDto1 = BeanCopyUtil.resultVO(article, AddArticleDto.class);
        LambdaQueryWrapper<ArticleTag> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(ArticleTag::getArticleId,id);
        List<ArticleTag> tagList = articleTagService.list(wrapper1);
        List<Long> longList = tagList.stream()
                .map(tag -> tag.getTagId())
                .collect(Collectors.toList());
        addArticleDto1.setTags(longList);
        return ResponseResult.okResult(addArticleDto1);

    }

    @Override
    public ResponseResult deleteList(Long id) {
        articleMapper.deleteById(id);
        return ResponseResult.okResult();












    }


}